package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

func getFileList(root string) {
	fv, e := os.OpenFile(root+"htmls.dart", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
	if e != nil {
		fmt.Println(e)
		return
	}
	fv.WriteString("class Htmls{\n")
	e = filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
		if f == nil {
			return err
		}
		if f.IsDir() || isHiddenDir(path) {
			return nil
		}
		if f.Name()[len(f.Name())-5:] != ".html" {
			return nil
		}
		_, e := fv.WriteString("static String str_" + f.Name()[:len(f.Name())-5] + " ='''")
		if e != nil {
			fmt.Println(e)
			return nil
		}
		fi, e := os.OpenFile(path, os.O_RDONLY, 0644)
		if e != nil {
			fmt.Println(e)
			return nil
		}
		_, e = io.Copy(fv, fi)
		if e != nil {
			fmt.Println(e)
			return nil
		}
		fv.WriteString("''';\n")
		fi.Close()
		fmt.Println(path)
		return nil
	})
	fv.WriteString("}")
	fv.Close()
	if e != nil {
		fmt.Println(e)
		return
	}
}
func main() {
	d, e := os.Getwd()
	if e != nil {
		fmt.Println(e)
		return
	}
	getFileList(getrpath(d))
}

func getrpath(p string) string {
	if p[len(p)-1:] == "/" {
		return p
	}
	return p + "/"
}
func isHiddenDir(s string) bool {
	if s[:1] == "." {
		return true
	}
	if strings.Contains(s, "/.") {
		return true
	}
	return false
}
